import { lazy } from 'react';

// Lazy load page components for better performance
const DashboardPage = lazy(() => import('@/pages/dashboard'));
const LoginPage = lazy(() => import('@/pages/auth/login'));
const RegisterPage = lazy(() => import('@/pages/auth/register'));
const VideoToolsPage = lazy(() => import('@/pages/video-tools'));
const VideoEditorPage = lazy(() => import('@/pages/video-editor'));
const BatchProcessPage = lazy(() => import('@/pages/batch-process'));
const AnalyticsPage = lazy(() => import('@/pages/analytics'));
const SettingsPage = lazy(() => import('@/pages/settings'));
const ProfilePage = lazy(() => import('@/pages/profile'));
const FormatConverterPage = lazy(() => import('@/pages/video-tools/format-converter'));
const VideoCompressorPage = lazy(() => import('@/pages/video-tools/video-compressor'));
const AudioExtractorPage = lazy(() => import('@/pages/video-tools/audio-extractor'));

// Route configuration
export interface RouteConfig {
  path: string;
  element: React.ComponentType;
  title: string;
  requiresAuth?: boolean;
  isPublic?: boolean;
  sidebar?: {
    icon: string;
    label: string;
    group?: string;
    order?: number;
  };
}

export const routes: RouteConfig[] = [
  // Auth routes (public)
  {
    path: '/auth/login',
    element: LoginPage,
    title: '登录',
    isPublic: true,
  },
  {
    path: '/auth/register',
    element: RegisterPage,
    title: '注册',
    isPublic: true,
  },

  // Main app routes (protected)
  {
    path: '/',
    element: DashboardPage,
    title: '仪表板',
    requiresAuth: true,
    sidebar: {
      icon: 'LayoutDashboard',
      label: '仪表板',
      group: 'main',
      order: 1,
    },
  },
  {
    path: '/dashboard',
    element: DashboardPage,
    title: '仪表板',
    requiresAuth: true,
    sidebar: {
      icon: 'LayoutDashboard',
      label: '仪表板',
      group: 'main',
      order: 1,
    },
  },
  {
    path: '/video-tools',
    element: VideoToolsPage,
    title: '视频工具',
    requiresAuth: true,
    sidebar: {
      icon: 'Video',
      label: '视频工具',
      group: 'tools',
      order: 2,
    },
  },
  {
    path: '/video-tools/format-converter',
    element: FormatConverterPage,
    title: '格式转换',
    requiresAuth: true,
  },
  {
    path: '/video-tools/video-compressor',
    element: VideoCompressorPage,
    title: '视频压缩',
    requiresAuth: true,
  },
  {
    path: '/video-tools/audio-extractor',
    element: AudioExtractorPage,
    title: '音频提取',
    requiresAuth: true,
  },
  {
    path: '/video-editor',
    element: VideoEditorPage,
    title: '视频编辑器',
    requiresAuth: true,
    sidebar: {
      icon: 'Film',
      label: '视频编辑器',
      group: 'tools',
      order: 3,
    },
  },
  {
    path: '/batch-process',
    element: BatchProcessPage,
    title: '批量处理',
    requiresAuth: true,
    sidebar: {
      icon: 'Package',
      label: '批量处理',
      group: 'tools',
      order: 4,
    },
  },
  {
    path: '/analytics',
    element: AnalyticsPage,
    title: '数据分析',
    requiresAuth: true,
    sidebar: {
      icon: 'BarChart3',
      label: '数据分析',
      group: 'insights',
      order: 5,
    },
  },
  {
    path: '/settings',
    element: SettingsPage,
    title: '设置',
    requiresAuth: true,
    sidebar: {
      icon: 'Settings',
      label: '设置',
      group: 'system',
      order: 7,
    },
  },
  {
    path: '/profile',
    element: ProfilePage,
    title: '个人资料',
    requiresAuth: true,
    sidebar: {
      icon: 'User',
      label: '个人资料',
      group: 'system',
      order: 6,
    },
  },
];

// Get routes grouped by sidebar groups
export const getSidebarRoutes = () => {
  const routesWithSidebar = routes.filter(route => route.sidebar);

  const grouped = routesWithSidebar.reduce(
    (groups, route) => {
      const group = route.sidebar!.group || 'default';
      if (!groups[group]) {
        groups[group] = [];
      }
      groups[group].push(route);
      return groups;
    },
    {} as Record<string, RouteConfig[]>
  );

  // Sort routes within each group by order
  Object.keys(grouped).forEach(group => {
    grouped[group].sort((a, b) => (a.sidebar?.order || 0) - (b.sidebar?.order || 0));
  });

  return grouped;
};

// Get route by path
export const getRouteByPath = (path: string): RouteConfig | undefined => {
  return routes.find(route => route.path === path);
};

// Check if route requires authentication
export const requiresAuth = (path: string): boolean => {
  const route = getRouteByPath(path);
  return route?.requiresAuth !== false && !route?.isPublic;
};

// Check if route is public
export const isPublicRoute = (path: string): boolean => {
  const route = getRouteByPath(path);
  return route?.isPublic === true;
};
